<?php

namespace app\index\controller;

use app\index\model\Comment;
use think\Controller;
use think\Db;
use app\util\Utils;
use app\util\Alipay;
use app\util\WxPay;
use think\Log;
use app\index\model\UshareCustom;
use app\index\model\Order;
use app\v300\controller\H5wxpayController;
use app\v300\controller\AlipayController;
use app\index\model\Ushare;
use app\index\model\Custom;
use app\index\model\Servicenews;
use app\index\model\Item;
use app\index\model\Store;

/*
 * 优享活动失败退款
 */

class UshareController extends Controller {
    /*
     * 定时更新订单状态
     *
     * 订单状态为服务中(7)和美容师服务完成(8)时，如果客户没有确认服务完成，自动更新为服务完成(9)
     */

    public function UshareorderRefund() {
        $usharecustom=UshareCustom::where('status','=',  UshareCustom::USHARE_STATUS_2)
                ->field('id,ord_num,pay_money,pay_type,status,end_time,ushare_id,ctm_id,store_id,wx_openid,pay_time')
                ->select();
        for($i=0;$i<count($usharecustom);$i++){
            $status=$usharecustom[$i]['status'];
            $end_time=$usharecustom[$i]['end_time'];
            $ord_num=$usharecustom[$i]['ord_num'];
            $pay_money=$usharecustom[$i]['pay_money'];
            $pay_type=$usharecustom[$i]['pay_type'];
            $store_id=$usharecustom[$i]['store_id'];
            $order_id=$usharecustom[$i]['id'];
            $now=time();
            $ushare=Ushare::where('id','=',$usharecustom[$i]['ushare_id'])->field('item_id')->find();
            $itemdata=Item::where('id','=',$ushare['item_id'])->field('item_name')->find();
            $customdata = Custom::where('id', '=', $usharecustom[$i]['ctm_id'])
                ->field('custom_phone')
                ->find();
            
            if($status==UshareCustom::USHARE_STATUS_2 && $end_time<$now){
                if($pay_type==Order::ORDTYPE_3){//app支付宝
                    $alipay = new Alipay();
                    $alipayrefunddata = $alipay->refund($ord_num, $pay_money);
                    if ($alipayrefunddata['code'] == 0) {
                        $alipayfindrefund = $alipay->findrefund($ord_num);
                        if ($alipayfindrefund['code'] == 0) {
                            $updatastatus['alipay_refund_no'] = $alipayfindrefund['data']->trade_no;
                            $updatastatus['status'] = UshareCustom::USHARE_STATUS_5;
                            $updatastatus['update_time'] = time();
                            $updateusharecustom = UshareCustom::where('ord_num','=',$ord_num)->update($updatastatus);
                            //退款成功发送推送
                            $content = "很遗憾，您发起的【[".$itemdata['item_name']."]】优享助力活动助力失败，已成功退款。";
                            $msgContent = Utils::getColoredString($content);
                            $pushContent = Utils::getPushString($content);
                            $alias = $customdata['custom_phone'] . $store_id;
                            $userid = $usharecustom[$i]['ctm_id'];
                            Utils::sendServicenews(Servicenews::TITLEID_15, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_2, $store_id, [
                                'title' => '活动消息',
                                'content' => $pushContent,
                                'alias_type' => Servicenews::ALIAS_TYPE_2,
                                'alias' => $alias,
                            ]);
                        }
                    }
                }elseif ($pay_type==Order::ORDTYPE_4) {//app微信
                    $wxpay = new WxPay();
                    $wxrefunddata = $wxpay->WxRefund($ord_num, $pay_money, $pay_money);
                }elseif ($pay_type==Order::ORDTYPE_5) {//网页微信
                    $wxpay=new H5wxpayController();
                    $wxpay->WxRefund($ord_num, $pay_money, $pay_money);
                }elseif ($pay_type==Order::ORDTYPE_6) {//网页支付宝
                    $alipay=new AlipayController();
                    $alipayrefunddata=$alipay->refund($ord_num, $pay_money);
                    if ($alipayrefunddata['code'] == 0) {
                        $alipayfindrefund = $alipay->findrefund($ord_num);
                        if ($alipayfindrefund['code'] == 0) {
                            $updatastatus['alipay_refund_no'] = $alipayfindrefund['data']->trade_no;
                            $updatastatus['status'] = UshareCustom::USHARE_STATUS_5;
                            $updatastatus['update_time'] = time();
                            $updateusharecustom = UshareCustom::where('ord_num','=',$ord_num)->update($updatastatus);
                            //退款成功发送推送
                            $content = "很遗憾，您发起的【[".$itemdata['item_name']."]】优享助力活动助力失败，已成功退款。";
                            $msgContent = Utils::getColoredString($content);
                            $pushContent = Utils::getPushString($content);
                            $alias = $customdata['custom_phone'] . $store_id;
                            $userid = $usharecustom[$i]['ctm_id'];
                            Utils::sendServicenews(Servicenews::TITLEID_15, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_2, $store_id, [
                                'title' => '活动消息',
                                'content' => $pushContent,
                                'alias_type' => Servicenews::ALIAS_TYPE_2,
                                'alias' => $alias,
                            ]);
                        }
                    }
                }elseif ($pay_type==Order::ORDTYPE_7) {//小程序
                    $wxpay = new WxPay();
                    $wxrefunddata = $wxpay->XcxWxRefund($ord_num, $pay_money, $pay_money);
                }
                //退款成功发送公众号消息
                $storedata = Store::where('id','=',$store_id)->field('store_name')->find();
                //发送模板信息
                $ImgPath = config('appconfig.ApiPath');
                $postdata['orderId'] = $usharecustom[$i]['id'];
                $postdata['uShareId'] = $usharecustom[$i]['ushare_id'];
                $postdata['ctmId'] = $usharecustom[$i]['ctm_id'];
                $postdata['storeId'] = $store_id;
                $postdata['openId'] = $usharecustom[$i]['wx_openid'];
                $postdata['spendTime'] = date('Y-m-d H:i:s',$usharecustom[$i]['pay_time']);
                $postdata['spendStore'] = $storedata['store_name'];
                $postdata['spendAccount'] = $pay_money;
                $postdata['programName'] = $itemdata['item_name'];
                $postdata['orderStatus'] = 1;
                $postdata['sourceType'] = 1;
                Utils::curlPost($ImgPath.'message/sendWechatMessage',$postdata);
            }
        }
    }
   
}
